<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>chessroom</title>
<script src="vue.js"></script>
<style>
div.locX{
	width: 60px;
	display: inline-block;
	font-size: 12px;
	color: black;
}
div.locXStart{
	width: 56px;
}
div.red{
	color: red;
}
div.right, div.left{
	display: inline-block;
}
div.right{
	width: 450px;
	vertical-align: top;
}
div.right > *{
	display: inline-block;
	width: 200px;
	min-width: 200px;
	margin: 0 10px;
}
div.rival{
	font-size: 40px;
	text-align: center;
	margin: 20px 10px;
}
div.rival-name{
	text-align: center;
}
div.rival-name > div{
	width: 200px;
	font-size: 16px;
	display: inline-block;
	vertical-align: top;
	line-height: 20px;
	margin: 10px 0;
}
div.rival-name img{
	width: 60px;
	height: 60px;
	margin: 0;
	display: inline-block;
	border-radius: 40px;
}
div.rival-input input{
	width: 100%;
	text-align: center;
}
div.log{
	font-size: 20px;
	text-align: center;
	margin: 5px 10px;
}
body{
	background-color: #eeeeee;
}
</style>
</head>
<body>
<div class="left">
	<div class="locX locXStart"></div><div class="locX">1</div><div class="locX">2</div><div class="locX">3</div><div class="locX">4</div><div class="locX">5</div><div class="locX">6</div><div class="locX">7</div><div class="locX">8</div><div class="locX">9</div><br>
	<canvas id="canvas" width="600" height="660"></canvas><br>
	<div class="locX locXStart"></div><div class="locX red">九</div><div class="locX red">八</div><div class="locX red">七</div><div class="locX red">六</div><div class="locX red">五</div><div class="locX red">四</div><div class="locX red">三</div><div class="locX red">二</div><div class="locX red">一</div><br>
</div>
<div class="right" id="app">
<div class="rival red">红方</div><div class="rival">黑方</div><br>
<div class="rival-name"><img src="default.jpg"><div>{{red_rival_name}}</div></div><div class="rival-name"><img src="default.jpg"><div>{{black_rival_name}}</div></div><br>
<div class="rival-input"><input id="input-red" v-on:keypress="onInput(0)" v-model="red_input"></div><div class="rival-input"><input id="input-black" v-on:keypress="onInput(1)" v-model="black_input"></div><br>
<template v-for="log in logs">
<div class="log">{{log}}</div>
</template>
</div>
<script>
//vue框架相关
var vue = new Vue({
	el: "#app",
	data: {
		red_rival_name: "颜姬Channel",
		black_rival_name: "颜姬Studio",
		red_input: "",
		black_input: "",
		logs: []
	},
	methods: {
		onInput: function(color){
			var keyCode = event.keyCode;
			if(keyCode==13){
				var stepResult;
				if(color==0){
					stepResult = step(this.red_input,0);
				}else{
					stepResult = step(this.black_input,1);
				}
				if(stepResult){
					this.logs.push(stepResult);
					if(this.logs.length > 26){
						this.logs.shift();
						this.logs.shift();
					}
					if(color==0){
						document.getElementById("input-black").focus();
					}else{
						document.getElementById("input-red").focus();
					}
				}
				this.red_input = "";
				this.black_input = "";
			}
		}
	}
});
//象棋棋子类定义
function Chess(color,type,locX,locY){
	this.color = color;
	this.type = type;
	this.locX = locX;
	this.locY = locY;
	var active = true;
	this.disActive = disActive;
	function disActive(){
		active = false;
	}
	this.isActive = isActive;
	function isActive(){
		return active;
	}
	this.getName = getName;
	function getName(){
		if(type=="ju"){
			return "車";
		}else if(type=="ma"){
			return "馬";
		}else if(type=="pao"){
			return "炮";
		}
		if(color==1){
			if(type=="xiang"){
				return "象";
			}else if(type=="shi"){
				return "士";
			}else if(type=="shuai"){
				return "將";
			}else if(type=="bing"){
				return "卒";
			}
		}else if(color==0){
			if(type=="xiang"){
				return "相";
			}else if(type=="shi"){
				return "仕";
			}else if(type=="shuai"){
				return "帥";
			}else if(type=="bing"){
				return "兵";
			}
		}
	}
}
//象棋类型定义
var chessTypes = ["ju","ma","xiang","shi","shuai","pao","bing"];
//象棋棋盘（及其上所有棋子定义）
var chessGame = [new Chess(1,"ju",0,0), new Chess(1,"ma",1,0), 
	new Chess(1,"xiang",2,0),new Chess(1,"shi",3,0),
	new Chess(1,"shuai",4,0),new Chess(1,"shi",5,0),
	new Chess(1,"xiang",6,0),new Chess(1,"ma",7,0),
	new Chess(1,"ju",8,0),new Chess(1,"pao",1,2),
	new Chess(1,"pao",7,2),new Chess(1,"bing",0,3),
	new Chess(1,"bing",2,3),new Chess(1,"bing",4,3),
	new Chess(1,"bing",6,3),new Chess(1,"bing",8,3),
	new Chess(0,"ju",0,9), new Chess(0,"ma",1,9), 
	new Chess(0,"xiang",2,9),new Chess(0,"shi",3,9),
	new Chess(0,"shuai",4,9),new Chess(0,"shi",5,9),
	new Chess(0,"xiang",6,9),new Chess(0,"ma",7,9),
	new Chess(0,"ju",8,9),new Chess(0,"pao",1,7),
	new Chess(0,"pao",7,7),new Chess(0,"bing",0,6),
	new Chess(0,"bing",2,6),new Chess(0,"bing",4,6),
	new Chess(0,"bing",6,6),new Chess(0,"bing",8,6)];
var currentX,currentY;
var previousX,previousY;
//根据象棋类型的中文获取对应的象棋类型枚举
function getChessType(chessTypeName){
	if(chessTypeName == '车' || chessTypeName == '車'){
		return "ju";
	}else if(chessTypeName == '马' || chessTypeName == '馬'){
		return "ma";
	}else if(chessTypeName == '相' || chessTypeName == '象'){
		return "xiang";
	}else if(chessTypeName == '士' || chessTypeName == '仕'){
		return "shi";
	}else if(chessTypeName == '帅' || chessTypeName == '帥' || chessTypeName == '将' || chessTypeName == '將'){
		return "shuai";
	}else if(chessTypeName == '炮' || chessTypeName == '砲'){
		return "pao";
	}else if(chessTypeName == '兵' || chessTypeName == '卒'){
		return "bing";
	}
}
//根据象棋类型枚举获取对应的象棋类型中文（简体）
function getChessName(chessType, color){
	if(chessType=="ju"){
		return "车";
	}else if(chessType=="ma"){
		return "马";
	}else if(chessType=="pao"){
		return "炮";
	}
	if(color==1){
		if(chessType=="xiang"){
			return "象";
		}else if(chessType=="shi"){
			return "士";
		}else if(chessType=="shuai"){
			return "将";
		}else if(chessType=="bing"){
			return "卒";
		}
	}else if(color==0){
		if(chessType=="xiang"){
			return "相";
		}else if(chessType=="shi"){
			return "仕";
		}else if(chessType=="shuai"){
			return "帅";
		}else if(chessType=="bing"){
			return "兵";
		}
	}
}
//根据象棋类型、相对位置和红黑方获取符合条件的象棋棋子
function getChessFromRelativeLoc(chessType,relativeLoc,color){
	var chesses = [];
	//首先获取符合象棋类型和红黑方条件的棋子
	for(var i=0;i<chessGame.length;i++){
		if(chessGame[i].isActive() && chessType == chessGame[i].type && color == chessGame[i].color){
			chesses.push(chessGame[i]);
		}
	}
	//无符合象棋类型和红黑方条件的棋子直接返回空
	if(!chesses){
		return;
	//对于“前”、“后”开头的行棋语句且两棋子位于同一列则返回对应在前或在后棋子
	}else if(relativeLoc == -2 || relativeLoc == -1){
		if(chesses.length < 2){
			return;
		}
		var chess1;
		var chess2;
		for(var i=0;i<chesses.length;i++){
			for(var j=i+1;j<chesses.length;j++){
				if(chesses[i].locX == chesses[j].locX){
					chess1 = chesses[i];
					chess2 = chesses[j];
				}
			}
		}
		if(!chess1){
			return;
		}
		if(relativeLoc == -1){
			if(color == 0){
				if(chess1.locY < chess2.locY){
					return chess1;
				}else if(chess1.locY > chess2.locY){
					return chess2;
				}
			}else if(color == 1){
				if(chess1.locY < chess2.locY){
					return chess2;
				}else if(chess1.locY > chess2.locY){
					return chess1;
				}
			}
		}else if(relativeLoc == -2){
			if(color == 0){
				if(chess1.locY < chess2.locY){
					return chess2;
				}else if(chess1.locY > chess2.locY){
					return chess1;
				}
			}else if(color == 1){
				if(chess1.locY < chess2.locY){
					return chess1;
				}else if(chess1.locY > chess2.locY){
					return chess2;
				}
			}
		}
	//对于普通的行棋语句则返回对应列上唯一的棋子
	}else{
		var returnChess;
		for(var i=0;i<chesses.length;i++){
			var chess = chesses[i];
			if(color == 0){
				if(chess.locX == 9-relativeLoc){
					if(!returnChess){
						returnChess = chess;
					}else{
						return;
					}
				}
			}else if(color == 1){
				if(chess.locX == relativeLoc-1){
					if(!returnChess){
						returnChess = chess;
					}else{
						return;
					}
				}
			}
		}
		return returnChess;
	}
}
//根据坐标获取棋子
function getChessFromLoc(locX,locY){
	for(var i=0;i<chessGame.length;i++){
		if(chessGame[i].isActive() && chessGame[i].locX == locX && chessGame[i].locY == locY){
			return chessGame[i];
		}
	}
}
//移动棋子到指定方向和地点，成功返回空，失败返回提示信息
function moveChess(chess,direction,dest){
	var destX;
	var destY;
	if(chess.type == "ju"){
		if(chess.color == 0){
			if(direction == 1){
				destX = chess.locX;
				destY = chess.locY - dest;
			}else if(direction == 0){
				destX = 9-dest;
				destY = chess.locY;
			}else if(direction == -1){
				destX = chess.locX;
				destY = chess.locY + dest;
			}
		}else if(chess.color == 1){
			if(direction == 1){
				destX = chess.locX;
				destY = chess.locY + dest;
			}else if(direction == 0){
				destX = dest-1;
				destY = chess.locY;
			}else if(direction == -1){
				destX = chess.locX;
				destY = chess.locY - dest;
			}
		}
		if(destX < 0 || destX > 8 || destY < 0 || destY > 9){
			return "超出棋盘范围";
		}
		if(destX == chess.locX && destY == chess.locY){
			return "棋子没有移动";
		}
		var startX = destX < chess.locX ? destX : chess.locX;
		var startY = destY < chess.locY ? destY : chess.locY;
		var endX = destX < chess.locX ? chess.locX : destX;
		var endY = destY < chess.locY ? chess.locY : destY;
		for(var x=startX; x<=endX; x++){
			for(var y=startY; y<=endY; y++){
				if((x != chess.locX || y != chess.locY) && (x != destX || y != destY)){
					var chessPath = getChessFromLoc(x,y);
					if(chessPath){
						return "车的移动路径上有棋子阻挡";
					}
				}
				
			}
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}else if(chess.type == "ma"){
		if(chess.color == 0){
			if(direction == 1){
				destX = 9-dest;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY - 2;
				}else if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY - 1;
				}else{
					return "马无法走到对应列";
				}
			}else if(direction == 0){
				return "马不能平走";
			}else if(direction == -1){
				destX = 9-dest;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY + 2;
				}else if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY + 1;
				}else{
					return "马无法走到对应列";
				}
			}
		}else if(chess.color == 1){
			if(direction == 1){
				destX = dest-1;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY + 2;
				}else if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY + 1;
				}else{
					return "马无法走到对应列";
				}
			}else if(direction == 0){
				return "马不能平走";
			}else if(direction == -1){
				destX = dest-1;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY - 2;
				}else if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY - 1;
				}else{
					return "马无法走到对应列";
				}
			}
		}
		if(destX < 0 || destX > 8 || destY < 0 || destY > 9){
			return "超出棋盘范围";
		}
		var middleX;
		var middleY;
		if(chess.locX - destX == 1 || chess.locX - destX == -1){
			middleX = chess.locX;
		}else if(chess.locX - destX == 2 || chess.locX - destX == -2){
			middleX = (chess.locX + destX) / 2;
		}
		if(chess.locY - destY == 1 || chess.locY - destY == -1){
			middleY = chess.locY;
		}else if(chess.locY - destY == 2 || chess.locY - destY == -2){
			middleY = (chess.locY + destY) / 2;
		}
		var chessMiddle = getChessFromLoc(middleX,middleY);
		if(chessMiddle){
			return "撇马腿";
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}else if(chess.type == "pao"){
		if(chess.color == 0){
			if(direction == 1){
				destX = chess.locX;
				destY = chess.locY - dest;
			}else if(direction == 0){
				destX = 9-dest;
				destY = chess.locY;
			}else if(direction == -1){
				destX = chess.locX;
				destY = chess.locY + dest;
			}
		}else if(chess.color == 1){
			if(direction == 1){
				destX = chess.locX;
				destY = chess.locY + dest;
			}else if(direction == 0){
				destX = dest-1;
				destY = chess.locY;
			}else if(direction == -1){
				destX = chess.locX;
				destY = chess.locY - dest;
			}
		}
		if(destX < 0 || destX > 8 || destY < 0 || destY > 9){
			return "超出棋盘范围";
		}
		if(destX == chess.locX && destY == chess.locY){
			return "棋子没有移动";
		}
		var startX = destX < chess.locX ? destX : chess.locX;
		var startY = destY < chess.locY ? destY : chess.locY;
		var endX = destX < chess.locX ? chess.locX : destX;
		var endY = destY < chess.locY ? chess.locY : destY;
		var chessPathCount = 0;
		for(var x=startX; x<=endX; x++){
			for(var y=startY; y<=endY; y++){
				if((x != chess.locX || y != chess.locY) && (x != destX || y != destY)){
					var chessPath = getChessFromLoc(x,y);
					if(chessPath){
						chessPathCount++;
					}
				}
				
			}
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessPathCount == 0){
			if(chessDest){
				return "目的地有棋子阻挡";
			}
		}else if(chessPathCount == 1){
			if(chessDest){
				if(chessDest.color != chess.color){
					chessDest.disActive();
				}else if(chessDest.color == chess.color){
					return "目的地有己方棋子阻挡";
				}
			}else{
				return "炮的目的地无棋子且移动路径上有棋子";
			}
		}else{
			return "炮的移动路径上有两个或以上的棋子";
		}
	}else if(chess.type == "bing"){
		if(chess.color == 0){
			if(direction == 1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY - dest;
				}else{
					return "兵只能前进一格";
				}
			}else if(direction == 0){
				if(chess.locY <= 4){
					destX = 9-dest;
					destY = chess.locY;
				}else{
					return "未过河的兵不能平走";
				}
			}else if(direction == -1){
				return "兵不能后退";
			}
		}else if(chess.color == 1){
			if(direction == 1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY + dest;
				}else{
					return "卒只能前进一格";
				}
			}else if(direction == 0){
				if(chess.locY >= 5){
					destX = dest-1;
					destY = chess.locY;
				}else{
					return "未过河的卒不能平走";
				}
			}else if(direction == -1){
				return "卒不能后退";
			}
		}
		if(destX < 0 || destX > 8 || destY < 0 || destY > 9){
			return "超出棋盘范围";
		}
		if(destX == chess.locX && destY == chess.locY){
			return "棋子没有移动";
		}
		if(destX - chess.locX >= 2 || destX - chess.locX <= -2){
			return "兵(卒)只能平移一格";
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}else if(chess.type == "xiang"){
		if(chess.color == 0){
			if(direction == 1){
				destX = 9-dest;
				if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY - 2;
				}else{
					return "相无法走到对应列";
				}
			}else if(direction == 0){
				return "相不能平走";
			}else if(direction == -1){
				destX = 9-dest;
				if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY + 2;
				}else{
					return "相无法走到对应列";
				}
			}
			if(destY <= 4){
				return "相不能过河";
			}
		}else if(chess.color == 1){
			if(direction == 1){
				destX = dest-1;
				if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY + 2;
				}else{
					return "象无法走到对应列";
				}
			}else if(direction == 0){
				return "象不能平走";
			}else if(direction == -1){
				destX = dest-1;
				if(chess.locX - destX == 2 || chess.locX - destX == -2){
					destY = chess.locY - 2;
				}else{
					return "象无法走到对应列";
				}
			}
			if(destY >= 5){
				return "象不能过河";
			}
		}
		if(destX < 0 || destX > 8 || destY < 0 || destY > 9){
			return "超出棋盘范围";
		}
		var middleX = (chess.locX + destX) / 2;
		var middleY = (chess.locY + destY) / 2;
		var chessMiddle = getChessFromLoc(middleX,middleY);
		if(chessMiddle){
			return "塞象眼";
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}else if(chess.type == "shi"){
		if(chess.color == 0){
			if(direction == 1){
				destX = 9-dest;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY - 1;
				}else{
					return "仕无法走到对应列";
				}
			}else if(direction == 0){
				return "仕不能平走";
			}else if(direction == -1){
				destX = 9-dest;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY + 1;
				}else{
					return "仕无法走到对应列";
				}
			}
			if(destY <= 6 || destY >= 10 || destX <= 2 || destX >= 6){
				return "仕不能走出九宫";
			}
		}else if(chess.color == 1){
			if(direction == 1){
				destX = dest-1;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY + 1;
				}else{
					return "士无法走到对应列";
				}
			}else if(direction == 0){
				return "士不能平走";
			}else if(direction == -1){
				destX = dest-1;
				if(chess.locX - destX == 1 || chess.locX - destX == -1){
					destY = chess.locY - 1;
				}else{
					return "士无法走到对应列";
				}
			}
			if(destY <= -1 || destY >= 3 || destX <= 2 || destX >= 6){
				return "士不能走出九宫";
			}
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}else if(chess.type == "shuai"){
		if(chess.color == 0){
			if(direction == 1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY - dest;
				}else{
					return "帅只能前进一格";
				}
			}else if(direction == 0){
				destX = 9-dest;
				destY = chess.locY;
			}else if(direction == -1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY + dest;
				}else{
					return "帅只能后退一格";
				}
			}
			if(destY <= 6 || destY >= 10 || destX <= 2 || destX >= 6){
				return "帅不能走出九宫";
			}
		}else if(chess.color == 1){
			if(direction == 1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY + dest;
				}else{
					return "将只能前进一格";
				}
			}else if(direction == 0){
				destX = dest-1;
				destY = chess.locY;
			}else if(direction == -1){
				if(dest == 1){
					destX = chess.locX;
					destY = chess.locY - dest;
				}else{
					return "将只能后退一格";
				}
			}
			if(destY <= -1 || destY >= 3 || destX <= 2 || destX >= 6){
				return "将不能走出九宫";
			}
		}
		if(destX == chess.locX && destY == chess.locY){
			return "棋子没有移动";
		}
		if(destX - chess.locX >= 2 || destX - chess.locX <= -2){
			return "帅(将)只能平移一格";
		}
		var chessDest = getChessFromLoc(destX,destY);
		if(chessDest){
			if(chessDest.color != chess.color){
				chessDest.disActive();
			}else if(chessDest.color == chess.color){
				return "目的地有己方棋子阻挡";
			}
		}
	}
	previousX = chess.locX;
	previousY = chess.locY;
	currentX = destX;
	currentY = destY;
	chess.locX = destX;
	chess.locY = destY;
}
//检查游戏是否结束以及哪一方获胜
function checkEnd(color){
	for(var i=0;i<chessGame.length;i++){
		var chessShuai;
		var chessJiang;
		if(chessGame[i].isActive() && chessGame[i].type == "shuai"){
			if(chessGame[i].color == 0){
				chessShuai = chessGame[i];
			}else if(chessGame[i].color == 1){
				chessJiang = chessGame[i];
			}
		}
	}
	//哪一方将（帅）没了则另一方获胜
	if(!chessShuai){
		return 1;
	}else if(!chessJiang){
		return 0;
	}else{
		if(chessShuai.locX == chessJiang.locX){
			var x = chessShuai.locX;
			for(var y=chessJiang.locY+1; y<=chessShuai.locY-1; y++){
				var chessPath = getChessFromLoc(x,y);
				if(chessPath){
					return -1;
				}
			}
			//如果将帅照面则行棋方失败
			if(color == 0){
				return 1;
			}else if(color == 1){
				return 0;
			}
		}else{
			return -1;
		}
	}
}
//中文数字转阿拉伯数字
function toArabicNum(chineseNum){
	if(chineseNum == '一' || chineseNum == '1'){
		return 1;
	}else if(chineseNum == '二' || chineseNum == '2'){
		return 2;
	}else if(chineseNum == '三' || chineseNum == '3'){
		return 3;
	}else if(chineseNum == '四' || chineseNum == '4'){
		return 4;
	}else if(chineseNum == '五' || chineseNum == '5'){
		return 5;
	}else if(chineseNum == '六' || chineseNum == '6'){
		return 6;
	}else if(chineseNum == '七' || chineseNum == '7'){
		return 7;
	}else if(chineseNum == '八' || chineseNum == '8'){
		return 8;
	}else if(chineseNum == '九' || chineseNum == '9'){
		return 9;
	}else{
		return;
	}
}
//阿拉伯数字转中文数字
function toChineseNum(arabicNum){
	if(arabicNum == 1){
		return "一";
	}else if(arabicNum == 2){
		return "二";
	}else if(arabicNum == 3){
		return "三";
	}else if(arabicNum == 4){
		return "四";
	}else if(arabicNum == 5){
		return "五";
	}else if(arabicNum == 6){
		return "六";
	}else if(arabicNum == 7){
		return "七";
	}else if(arabicNum == 8){
		return "八";
	}else if(arabicNum == 9){
		return "九";
	}else{
		return;
	}
}
//行棋方法，入参为行棋语句及红黑方，返回为行棋结果
function step(input,color){
	//限制行棋语句为4个字
	if(input.length != 4){
		return;
	}
	var chessType;
	var relativeLoc;
	var inputResult = "";
	//对于“前”、“后”开头的行棋语句处理
	if(input.charAt(0) == '前' || input.charAt(0) == '后' || input.charAt(0) == '後'){
		//从第二个字获取象棋类型
		chessType = getChessType(input.charAt(1));
		//从第一个字获取相对位置，前为-1，后为-2
		if(input.charAt(0) == '前'){
			relativeLoc = -1;
			inputResult += "前";
		}else{
			relativeLoc = -2;
			inputResult += "后";
		}
		inputResult += getChessName(chessType, color);
	//对于普通的行棋语句处理
	}else{
		//从第一个字获取象棋类型
		chessType = getChessType(input.charAt(0));
		inputResult += getChessName(chessType, color);
		//从第二个字获取相对位置
		c1 = input.charAt(1);
		relativeLoc = toArabicNum(c1);
		if(!relativeLoc){
			return;
		}
		if(color == 0){
			inputResult += toChineseNum(relativeLoc);
		}else if(color == 1){
			inputResult += relativeLoc;
		}
	}
	if(!chessType){
		return;
	}
	//获取行棋语句指定要移动的象棋棋子
	var chess = getChessFromRelativeLoc(chessType,relativeLoc,color);
	if(!chess){
		//针对象或士位于同一列的情况
		if(chessType == 'xiang' || chessType == 'shi'){
			if(input.charAt(2) == '进' || input.charAt(2) == '進'){
				relativeLoc = -2;
			}else if(input.charAt(2) == '退'){
				relativeLoc = -1;
			}
		}
		chess = getChessFromRelativeLoc(chessType,relativeLoc,color);
	}
	//从第三个字获取棋子的移动方向
	var direction;
	if(input.charAt(2) == '进' || input.charAt(2) == '進'){
		direction = 1;
		inputResult += "进";
	}else if(input.charAt(2) == '平'){
		direction = 0;
		inputResult += "平";
	}else if(input.charAt(2) == '退'){
		direction = -1;
		inputResult += "退";
	}else{
		return;
	}
	//从第四个字获取棋子的移动目标
	var dest;
	c2 = input.charAt(3);
	dest = toArabicNum(c2);
	if(!dest){
		return;
	}
	if(color == 0){
		inputResult += toChineseNum(dest);
	}else if(color == 1){
		inputResult += dest;
	}
	//对可能合法但无法找到需要移动棋子的行棋语句进行提示
	if(!chess){
		alert("无法找到需要移动的棋子");
		return;
	}
	//获取棋子移动结果，为空则正常移动，否则移动失败
	var result = moveChess(chess,direction,dest);
	if(result){
		alert(result);
		return;
	}
	//棋子移动后重画棋盘和棋子
	drawChessGame();
	if(checkEnd(color) == 0){
		alert("红方胜");
	}else if(checkEnd(color) == 1){
		alert("黑方胜");
	}
	return inputResult;
}
//画布定义
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var scale = 60;
var radius = 25;
//画棋盘
function drawChessPlate(){
	context.beginPath();
	context.lineWidth="1";
	context.strokeStyle="black";
	context.moveTo(scale,scale);
	context.lineTo(scale*9,scale);
	context.lineTo(scale*9,scale*10);
	context.lineTo(scale,scale*10);
	context.lineTo(scale,scale);
	for(var i=2;i<=9;i++){
		context.moveTo(scale,scale*i);
		context.lineTo(scale*9,scale*i);
	}
	for(var i=2;i<=8;i++){
		context.moveTo(scale*i,scale);
		context.lineTo(scale*i,scale*5);
	}
	for(var i=2;i<=8;i++){
		context.moveTo(scale*i,scale*6);
		context.lineTo(scale*i,scale*10);
	}
	context.moveTo(scale*4,scale);
	context.lineTo(scale*6,scale*3);
	context.moveTo(scale*6,scale);
	context.lineTo(scale*4,scale*3);
	context.moveTo(scale*4,scale*8);
	context.lineTo(scale*6,scale*10);
	context.moveTo(scale*6,scale*8);
	context.lineTo(scale*4,scale*10);
	function drawCross(context,x,y){
		if(x!=1){
			context.moveTo(scale*x-20,scale*y-5);
			context.lineTo(scale*x-5,scale*y-5);
			context.lineTo(scale*x-5,scale*y-20);
			context.moveTo(scale*x-20,scale*y+5);
			context.lineTo(scale*x-5,scale*y+5);
			context.lineTo(scale*x-5,scale*y+20);
		}
		if(x!=9){
			context.moveTo(scale*x+20,scale*y-5);
			context.lineTo(scale*x+5,scale*y-5);
			context.lineTo(scale*x+5,scale*y-20);
			context.moveTo(scale*x+20,scale*y+5);
			context.lineTo(scale*x+5,scale*y+5);
			context.lineTo(scale*x+5,scale*y+20);
		}
	}
	drawCross(context,2,3);
	drawCross(context,8,3);
	drawCross(context,1,4);
	drawCross(context,3,4);
	drawCross(context,5,4);
	drawCross(context,7,4);
	drawCross(context,9,4);
	drawCross(context,1,7);
	drawCross(context,3,7);
	drawCross(context,5,7);
	drawCross(context,7,7);
	drawCross(context,9,7);
	drawCross(context,2,8);
	drawCross(context,8,8);
	context.stroke();
}
//画棋子
function drawChess(context,chess){
	context.beginPath();
	context.lineWidth="1";
	context.strokeStyle="black";
	context.arc(scale*(chess.locX+1),scale*(chess.locY+1),radius,0,2*Math.PI);
	context.stroke();
	context.fillStyle="yellow";
	context.fill();
	context.textAlign="center";
	context.fillStyle=chess.color==0?"red":"black";
	context.font="30px normal";
	context.fillText(chess.getName(),scale*(chess.locX+1),scale*(chess.locY+1)+11);
}
//画出刚刚移动的棋子的外框
function drawBox(locX,locY){
	context.beginPath();
	context.lineWidth="1";
	context.strokeStyle="red";
	context.moveTo(scale*(locX+1)-27,scale*(locY+1)-17);
	context.lineTo(scale*(locX+1)-27,scale*(locY+1)-27);
	context.lineTo(scale*(locX+1)-17,scale*(locY+1)-27);
	context.moveTo(scale*(locX+1)-27,scale*(locY+1)+17);
	context.lineTo(scale*(locX+1)-27,scale*(locY+1)+27);
	context.lineTo(scale*(locX+1)-17,scale*(locY+1)+27);
	context.moveTo(scale*(locX+1)+27,scale*(locY+1)-17);
	context.lineTo(scale*(locX+1)+27,scale*(locY+1)-27);
	context.lineTo(scale*(locX+1)+17,scale*(locY+1)-27);
	context.moveTo(scale*(locX+1)+27,scale*(locY+1)+17);
	context.lineTo(scale*(locX+1)+27,scale*(locY+1)+27);
	context.lineTo(scale*(locX+1)+17,scale*(locY+1)+27);
	context.stroke();
}
//画出完整的棋局（包括棋盘和所有棋子）
function drawChessGame(){
	context.clearRect(0,0,canvas.width,canvas.height);
	drawChessPlate();
	for(var i=0;i<chessGame.length;i++){
		if(chessGame[i].isActive()){
			drawChess(context,chessGame[i]);
		}
	}
	drawBox(previousX,previousY);
	drawBox(currentX,currentY);
}
drawChessGame();
document.getElementById("input-red").focus();
</script>
</body>
